package com.org.zp.ctrl.payquery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.ctrl.payquery.SearchPayList.QuerySearchSelectAsyncTask;
import com.org.zp.ctrl.search.SearchJobResultActivity;

/**
 * 薪酬查询 查询结果
 * 
 * @author GeYunjie
 * 
 */
public class PayQuryResultActivity extends BaseActivity {

	private TextView headerTitle; // 顶部条标题
	private Button mButtonBack; // 顶部条返回按钮
	private Button mButtonName; // 顶部条右侧按钮

	private RecApp mApp;
	private ProgressDialog progressDialog;

	private RelativeLayout your_condition_title;// 查询条件

	private LinearLayout tubiao_layout;


	// 查询的y轴最大最小值
	private int mYMax_q = 0;
	private int mYMin_q = 0;

	// 查询的y轴间隔
	private int mJiange = 0;

	// 查询的y轴坐标值
	private String[] mYdatas;

	// 查询的 结果值
	private String[] mX_query;
	private String[] mX_query1;

	// 比较的 结果值
	private String[] mX_bijiao = new String[] { "￥0", "￥0", "￥0", "￥0", "￥0" };
	private String[] mX_bijiao1 = new String[] { "0", "0", "0", "0", "0" };

	// 期望的坐标
	private float[] mQiwPic = { 0, 0 };

	// 期望线 y轴坐标值
	private float[] mPointQuery = { 0, 0, 0, 0, 0 };


	//画图 字体大小
	private int mTextSize = 0;
/*	//x轴点的间隔
	private int mPointDis = 0;*/
	
	private int mBili = 3;

	private DrawView myView;
	
	private String low = "";
	private String low_normal = "";
	private String normal = "";
	private String normal_high = "";
	private String high = "";
	
	private String address = "";
	private String positionname = "";
	private String hangye = "";
	private String positiontype = "";
	private String companysize = "";
	private String key = "";
	
	// 比较图的高度
	private float mdex = 60; // 如果比较后 mdex= 120，否则mdex=60
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				next();
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pay_query_result);
		
		if (!(ActivityTaskManager.getInstance().containsName("PayQuryResultActivity"))) {
			ActivityTaskManager.getInstance().putActivity("PayQuryResultActivity", this);
		}
		
		mApp = getMyApplication();

		Intent intent = getIntent();
		address = intent.getStringExtra("address");
		positionname = intent.getStringExtra("positionname");
		hangye = intent.getStringExtra("hangye");
		positiontype = intent.getStringExtra("positiontype");
		companysize = intent.getStringExtra("companysize");
		key = intent.getStringExtra("key");
		
		getComponentId();
		setQueryConditions();
		
		progressDialog = ProgressDialog.show(PayQuryResultActivity.this, "请等待...",  
				"正在加载信息，请稍后...", true, true);
		new QueryPayXinZiAsyncTask().execute(URLHelper.mPrefix);

	}
	
	class QueryPayXinZiAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			
			map.put("act", "remunerationQuery");
			
			// 关键字
			if (null != key && !key.equals("")) {

				map.put("key", key);
			}

			// 职位类别
			if (null != mApp.hPayId.get("position_name_second")
					&& !"".equals(mApp.hPayId.get("position_name_second")
							.toString())) {
				map.put("category", mApp.hPayId.get("position_name_second")
						.toString());
			}

			// 行业
			if (null != mApp.hPayId.get("position_hangye")
					&& !"".equals(mApp.hPayId.get("position_hangye")
							.toString())) {
				map.put("trade", mApp.hPayId.get("position_hangye").toString());
			}

			// 城市
			if (null != mApp.hPayId.get("position_address")
					&& !"".equals(mApp.hPayId.get("position_address")
							.toString())) {
				map.put("district", mApp.hPayId.get("position_address")
						.toString());
			}

			// 公司规模
			if (null != mApp.hPayId.get("position_companysize")
					&& !"".equals(mApp.hPayId.get("position_companysize")
							.toString())) {
				map.put("scale", mApp.hPayId.get("position_companysize")
						.toString());
			}

			// 公司性质
			if (null != mApp.hPayId.get("position_positiontype")
					&& !"".equals(mApp.hPayId.get("position_positiontype")
							.toString())) {
				map.put("nature", mApp.hPayId.get("position_positiontype")
						.toString());
			}
			
			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Log.e("TMG", "searchPosition result = null");
				progressDialog.dismiss();
			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Log.e("TMG", "searchPosition result = timeout");
				progressDialog.dismiss();
			} else {
				parseJson(result);
			}

		}
	}

	private void parseJson(String str) {
		Log.e("TMG", "searchPosition result = " + str);
		try {
			String succeed = new JSONObject(str).getString("succeed");
			if (succeed.equals("000")) {
				JSONObject json = new JSONObject(str);
				low = json.getString("low");
				low_normal = json.getString("low_normal");
				normal = json.getString("normal");
				normal_high = json.getString("normal_high");
				high = json.getString("high");
				
				Message msg0 = new Message();
				msg0.what = 0;
				handler.sendMessage(msg0);
				
			} else {
				low = "800";
				low_normal = "1700";
				normal = "2500";
				normal_high = "4200";
				high = "6500";
			}
			progressDialog.dismiss();

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void next(){
		
		getLineData();

		mQiwPic = getQiwang(Float.parseFloat(key));
		getPointQuery();
		
		//获取屏幕分辨率
		DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);        
        
        int mWidth = dm.widthPixels; //当前分辨率 宽度
        int mHeigth = dm.heightPixels; //当前分辨率高度
        Log.e("***", "mWidth = " + Integer.toString(mWidth));
        Log.e("***", "mHeigth = " + Integer.toString(mHeigth));
        
        if(320 == mWidth){
        	mTextSize = 14;
        	
        	mBili = 2;
        }else /*if(480 == mWidth)*/{
        	mTextSize = 18;
        	mBili = 3;
        }/*else{
        	mTextSize = 20;
        	mBili = 4;
        }*/

		myView = new DrawView(PayQuryResultActivity.this);

		tubiao_layout.addView(myView);
	}
	private void setQueryConditions() {

		TextView tv_address = (TextView) findViewById(R.id.pay_address);
		tv_address.setText(address);

		TextView tv_positionname = (TextView) findViewById(R.id.pay_position_name);
		tv_positionname.setText(positionname);

		TextView tv_hangye = (TextView) findViewById(R.id.pay_industry);
		tv_hangye.setText(hangye);

		TextView tv_positiontype = (TextView) findViewById(R.id.pay_xingzhi);
		tv_positiontype.setText(positiontype);

		TextView tv_companysize = (TextView) findViewById(R.id.pay_comsize);
		tv_companysize.setText(companysize);

		TextView tv_key = (TextView) findViewById(R.id.pay_salary);
		tv_key.setText(key);
	}

	public void getComponentId() {
		
		ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String, String>>();

		headerTitle = (TextView) findViewById(R.id.headTitle);
		headerTitle.setText("薪酬查询结果");

		mButtonBack = (Button) findViewById(R.id.back);
		mButtonBack.setOnClickListener(listener);
		mButtonBack.setText("  返回");

		mButtonName = (Button) findViewById(R.id.top_right_button);
		mButtonName.setVisibility(View.INVISIBLE);

		tubiao_layout = (LinearLayout) findViewById(R.id.tubiao_layout);

		your_condition_title = (RelativeLayout) findViewById(R.id.your_condition_title);
		your_condition_title.setOnClickListener(listener);

	}

	private View.OnClickListener listener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.your_condition_title:
				RelativeLayout pay_result_your_condition = (RelativeLayout) findViewById(R.id.pay_result_your_condition);
				if (pay_result_your_condition.getVisibility() == View.GONE) {
					pay_result_your_condition.setVisibility(View.VISIBLE);
				} else if (pay_result_your_condition.getVisibility() == View.VISIBLE) {
					pay_result_your_condition.setVisibility(View.GONE);
				}
				break;
			case R.id.back:
				ActivityTaskManager.getInstance().removeActivity(
						"PayQuryResultActivity");
				break;
			}
		}
	};

	private void getLineData() {

			// y轴最大值
			mYMax_q = SalaryUtil.getBaseNum(
					Integer.parseInt(high), 2);
			mYMin_q = SalaryUtil.getBaseNum(
					Integer.parseInt(low), 1);

			// y轴间隔
			mJiange = SalaryUtil.getY(mYMax_q, mYMin_q);


			// y轴坐标值
			mYdatas = new String[] { "0", Integer.toString(mJiange),
					Integer.toString(mJiange * 2),
					Integer.toString(mJiange * 3),
					Integer.toString(mJiange * 4),
					Integer.toString(mJiange * 5) };

			// 返回的
			mX_query = new String[] { ("￥" + low),
					("￥" + low_normal),
					("￥" + normal),
					("￥" + normal_high),
					("￥" + high) };

			mX_query1 = new String[] { (low),
					(low_normal), (normal),
					(normal_high), (high) };

	}

	public class DrawView extends View {

		public DrawView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			super.onDraw(canvas);

			Resources res = getResources();

			BitmapDrawable bmpDraw0 = (BitmapDrawable) res
					.getDrawable(R.drawable.xc_dizhonggaobg_2);
			Bitmap bmp0 = bmpDraw0.getBitmap();
			
			canvas.drawBitmap(bmp0, 15, 10, null);

			Paint paint0 = new Paint();
			paint0.setAntiAlias(true);
			paint0.setTextSize(mTextSize);
			paint0.setColor(Color.BLACK);
			canvas.drawText(mX_query[0], (85*mBili)/3, 38, paint0);
			canvas.drawText(mX_query[1], (154*mBili)/3, 38, paint0);
			canvas.drawText(mX_query[2], (240*mBili)/3, 38, paint0);
			canvas.drawText(mX_query[3], (312*mBili)/3, 38, paint0);
			canvas.drawText(mX_query[4], (395*mBili)/3, 38, paint0);

			canvas.drawText("月薪", 23, 38, paint0);

			BitmapDrawable bmpDraw2 = (BitmapDrawable) res
					.getDrawable(R.drawable.xc_xiantung);
			Bitmap bmp2 = bmpDraw2.getBitmap();
			
			Bitmap vB2 = Bitmap.createScaledBitmap( bmp2
                    , (bmp2 .getWidth() * mBili)/3
                    , (bmp2 .getHeight() * mBili)/3
                    , true
                    );

			
			canvas.drawBitmap(vB2, (65*mBili)/3, ((10*mBili)/3) + mdex, null);

			BitmapDrawable bmpDraw3 = (BitmapDrawable) res
					.getDrawable(R.drawable.xinchou_biaoji);
			Bitmap bmp3 = bmpDraw3.getBitmap();
			canvas.drawBitmap(bmp3, (mQiwPic[0]*mBili)/3, (mQiwPic[1]*mBili)/3, null);
			// canvas.drawBitmap(bmp1, (65 + 76), (10 + 30), null);

			Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setTextSize(mTextSize);
			paint.setColor(Color.BLACK);
			canvas.drawText("低端", (85*mBili)/3, ((185*mBili)/3) + mdex, paint);
			canvas.drawText("中低端", (154*mBili)/3, ((185*mBili)/3)+ mdex, paint);
			canvas.drawText("中端", (240*mBili)/3, ((185*mBili)/3)+ mdex, paint);
			canvas.drawText("中高端", (307*mBili)/3, ((185*mBili)/3)+ mdex, paint);
			canvas.drawText("高端", (390*mBili)/3, ((185*mBili)/3)+ mdex, paint);

			canvas.drawText(mYdatas[5], 10, ((30*mBili)/3)+ mdex, paint);
			canvas.drawText(mYdatas[4], 10, ((60*mBili)/3)+ mdex, paint);
			canvas.drawText(mYdatas[3], 10, ((90*mBili)/3)+ mdex, paint);
			canvas.drawText(mYdatas[2], 10, ((120*mBili)/3)+ mdex, paint);
			canvas.drawText(mYdatas[1], 10, ((150*mBili)/3)+ mdex, paint);

			Paint query_line_paint = new Paint();
			query_line_paint.setColor(Color.BLUE);
			/* 去锯齿 */
			query_line_paint.setAntiAlias(true);
			/* 设置paint的颜色 */
			/* 设置paint的 style 为STROKE：空心 */
			query_line_paint.setStyle(Paint.Style.FILL_AND_STROKE);
			/* 设置paint的外框宽度 */
			query_line_paint.setStrokeWidth(3);
			/* 画一个空心圆形 */

			canvas.drawCircle((100*mBili)/3, (mPointQuery[0]*mBili)/3, 2, query_line_paint);
			canvas.drawLine((100*mBili)/3, (mPointQuery[0]*mBili)/3, (170*mBili)/3, (mPointQuery[1]*mBili)/3,
					query_line_paint);

			canvas.drawCircle((170*mBili)/3, (mPointQuery[1]*mBili)/3, 2, query_line_paint);
			canvas.drawLine((170*mBili)/3, (mPointQuery[1]*mBili)/3, (255*mBili)/3, (mPointQuery[2]*mBili)/3,
					query_line_paint);

			canvas.drawCircle((255*mBili)/3, (mPointQuery[2]*mBili)/3, 2, query_line_paint);
			canvas.drawLine((255*mBili)/3, (mPointQuery[2]*mBili)/3, (325*mBili)/3, (mPointQuery[3]*mBili)/3,
					query_line_paint);

			canvas.drawCircle((325*mBili)/3, (mPointQuery[3]*mBili)/3, 2, query_line_paint);
			canvas.drawLine((325*mBili)/3, (mPointQuery[3]*mBili)/3, (405*mBili)/3, (mPointQuery[4]*mBili)/3,
					query_line_paint);

			canvas.drawCircle((405*mBili)/3, (mPointQuery[4]*mBili)/3, 2, query_line_paint);


		}
	}

	private float[] getQiwang(float qiwang) {
		float[] pic_x = { 0, 0 };

		if (qiwang < Float.parseFloat(mYdatas[1])) {
			pic_x[0] = 84;
			pic_x[1] = 115+ mdex;
		} else if ((qiwang > Float.parseFloat(mYdatas[1]))
				&& (qiwang <= Float.parseFloat(mYdatas[2]))) {
			pic_x[0] = 158;
			pic_x[1] = 85 + mdex;
		} else if ((qiwang > Float.parseFloat(mYdatas[2]))
				&& (qiwang <= Float.parseFloat(mYdatas[3]))) {
			pic_x[0] = 244;
			pic_x[1] = 55+ mdex;
		} else if ((qiwang > Float.parseFloat(mYdatas[3]))
				&& (qiwang <= Float.parseFloat(mYdatas[4]))) {
			pic_x[0] = 313;
			pic_x[1] = 25+ mdex;
		} else if ((qiwang > Float.parseFloat(mYdatas[4]))
				&& (qiwang <= Float.parseFloat(mYdatas[5]))) {
			pic_x[0] = 395;
			pic_x[1] = 0 + mdex;
		} else if (qiwang >= Float.parseFloat(mYdatas[5])) {
			pic_x[0] = 50+ (380*mBili)/3;
			pic_x[1] = -30+ mdex;
		}

		return pic_x;
	}

	private void getPointQuery() {

		for (int i = 0; i < 5; i++) {
			if (Float.parseFloat(mX_query1[i]) <= Float.parseFloat(mYdatas[1])) {
				mPointQuery[i] = 155+ mdex;

			} else if ((Float.parseFloat(mX_query1[i]) > Float
					.parseFloat(mYdatas[1]))
					&& (Float.parseFloat(mX_query1[i]) <= Float
							.parseFloat(mYdatas[2]))) {
				mPointQuery[i] = 125+ mdex;
			} else if ((Float.parseFloat(mX_query1[i]) > Float
					.parseFloat(mYdatas[2]))
					&& (Float.parseFloat(mX_query1[i]) <= Float
							.parseFloat(mYdatas[3]))) {
				mPointQuery[i] = 95+ mdex;
			} else if ((Float.parseFloat(mX_query1[i]) > Float
					.parseFloat(mYdatas[3]))
					&& (Float.parseFloat(mX_query1[i]) < Float
							.parseFloat(mYdatas[4]))) {
				mPointQuery[i] = 65+ mdex;
			} else if ((Float.parseFloat(mX_query1[i]) > Float
					.parseFloat(mYdatas[4]))
					&& (Float.parseFloat(mX_query1[i]) < Float
							.parseFloat(mYdatas[5]))) {
				mPointQuery[i] = 40+ mdex*2;
			} else if (Float.parseFloat(mX_query1[i]) > Float
					.parseFloat(mYdatas[5])) {
				mPointQuery[i] = 0+ mdex*2;
			}
		}

		for (int j = 4; j > 0; j--) {
			while (mPointQuery[j] >= mPointQuery[j - 1]) {
				mPointQuery[j] = mPointQuery[j] - 2;
				mPointQuery[j - 1] = mPointQuery[j - 1] + 3;
			}

		}

	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"PayQuryResultActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
